package com.liliudong.cursormate.service.impl;

import cn.dev33.satoken.stp.SaLoginConfig;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.liliudong.cursormate.common.constant.ConfigConst;
import com.liliudong.cursormate.common.constant.DataScopeTypeEnum;
import com.liliudong.cursormate.common.constant.SecurityConst;
import com.liliudong.cursormate.entity.SysUser;
import com.liliudong.cursormate.mapper.SysPermissionMapper;
import com.liliudong.cursormate.mapper.SysRoleMapper;
import com.liliudong.cursormate.service.SysUserService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


/**
 * @author Liudong.li
 * @description 针对表【sys_user(系统用户表)】的数据库操作Service实现
 * @createDate 2023-10-23 23:06:20
 */
@Service
@ConditionalOnProperty(name = {ConfigConst.CURSOR_MATE_POLICY}, havingValue = ConfigConst.CURSOR_MATE_POLICY_ADMIN)
public class SysUserServiceAdminImpl extends SysUserServiceImpl implements SysUserService {

    public SysUserServiceAdminImpl(SysRoleMapper sysRoleMapper,
                                   SysPermissionMapper sysPermissionMapper) {
        super(sysRoleMapper, sysPermissionMapper);
    }

    @Override
    public SaTokenInfo login(SysUser sysUser) {
        // 管理员直接登录成功
        StpUtil.login(0L,
                SaLoginConfig
                        .setExtra(SecurityConst.ORG_NO, SecurityConst.SUPER_ADMIN_ORG_CODE)
                        .setExtra(SecurityConst.DATA_SCOPE, DataScopeTypeEnum.OWN_AND_SUB_ORGANIZATION)
        );
        return StpUtil.getTokenInfo();
    }

    @Override
    public SysUser profile() {
        List<String> sysRoles = sysRoleMapper.selectRoleListInterceptorIgnore();
        List<String> permissionList = sysPermissionMapper.selectPermissionListInterceptorIgnore();
        SysUser sysUser = new SysUser();
        sysUser.setId(0L);
        sysUser.setUsername("admin");
        sysUser.setNickname("超级管理员");
        sysUser.setCreatedAt(LocalDateTime.now());
        sysUser.setUpdatedAt(LocalDateTime.now());
        sysUser.setCreatedBy(0L);
        sysUser.setUpdatedBy(0L);
        sysUser.setOrgNo(SecurityConst.SUPER_ADMIN_ORG_CODE);
        sysUser.setOrgNoBy(SecurityConst.SUPER_ADMIN_ORG_CODE);
        sysUser.setRoleCodeList(sysRoles);
        sysUser.setPermissionList(permissionList);
        return sysUser;
    }
}




